{% extends 'admin/admin_base.html' %}

{% block title %}试卷管理 - 后台管理系统{% endblock %}

{% block nav_exams %}active{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>试卷管理</h2>
        <div>
            <a href="{{ url_for('upload_exam.upload_exam') }}" class="btn btn-success me-2">
                <i class="bi bi-upload"></i> 上传试卷
            </a>
            <a href="{{ url_for('admin.exam_create') }}" class="btn btn-primary">
                <i class="bi bi-plus-lg"></i> 创建试卷
            </a>
        </div>
    </div>

    <!-- 搜索和筛选 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <input type="text" class="form-control" name="search" placeholder="搜索试卷名称..." value="{{ search }}">
                </div>
                <div class="col-md-4">
                    <select class="form-select" name="subject">
                        <option value="">所有科目</option>
                        <option value="python" {% if subject == 'python' %}selected{% endif %}>Python</option>
                        <option value="scratch" {% if subject == 'scratch' %}selected{% endif %}>Scratch</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <button type="submit" class="btn btn-primary">搜索</button>
                    <a href="{{ url_for('admin.exam_list') }}" class="btn btn-secondary">重置</a>
                </div>
            </form>
        </div>
    </div>

    <!-- 试卷列表 -->
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>试卷名称</th>
                            <th>科目</th>
                            <th>难度</th>
                            <th>题目数量</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for exam in exams %}
                        <tr>
                            <td>{{ exam.name }}</td>
                            <td>{{ exam.subject|upper }}</td>
                            <td>
                                {% for i in range(exam.difficulty|default(1, true)) %}
                                <i class="fas fa-star text-warning"></i>
                                {% endfor %}
                            </td>
                            <td>
                                {% set question_counts = {'single': 0, 'multiple': 0, 'boolean': 0} %}
                                {% for question in exam.questions %}
                                    {% if question.type == 'single' %}
                                        {% set _ = question_counts.update({'single': question_counts.single + 1}) %}
                                    {% elif question.type == 'multiple' %}
                                        {% set _ = question_counts.update({'multiple': question_counts.multiple + 1}) %}
                                    {% elif question.type == 'boolean' %}
                                        {% set _ = question_counts.update({'boolean': question_counts.boolean + 1}) %}
                                    {% endif %}
                                {% endfor %}
                                单选：{{ question_counts.single }}<br>
                                多选：{{ question_counts.multiple }}<br>
                                判断：{{ question_counts.boolean }}
                            </td>
                            <td>{{ exam.created_at }}</td>
                            <td>
                                <div class="btn-group">
                                    <a href="{{ url_for('admin.exam_view', exam_id=exam._id) }}" class="btn btn-sm btn-info">查看</a>
                                    <a href="{{ url_for('admin.exam_edit', exam_id=exam._id) }}" class="btn btn-sm btn-primary">编辑</a>
                                    <button type="button" class="btn btn-sm btn-danger" onclick="deleteExam('{{ exam._id }}')">删除</button>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if total_pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item {% if current_page == 1 %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('admin.exam_list', page=current_page-1, search=search, subject=subject) }}">上一页</a>
                    </li>
                    {% for page in range(1, total_pages + 1) %}
                    <li class="page-item {% if page == current_page %}active{% endif %}">
                        <a class="page-link" href="{{ url_for('admin.exam_list', page=page, search=search, subject=subject) }}">{{ page }}</a>
                    </li>
                    {% endfor %}
                    <li class="page-item {% if current_page == total_pages %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('admin.exam_list', page=current_page+1, search=search, subject=subject) }}">下一页</a>
                    </li>
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>

<!-- 删除确认对话框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                确定要删除这个试卷吗？此操作不可恢复。
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" method="post" style="display: inline;">
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function deleteExam(examId) {
    const modal = new bootstrap.Modal(document.getElementById('deleteModal'));
    const form = document.getElementById('deleteForm');
    form.action = "{{ url_for('admin.exam_delete', exam_id='') }}" + examId;
    modal.show();
}
</script>
{% endblock %}
